/**
 * Copyright (2017, ) Institute of Software, Chinese Academy of Sciences
 */
package cn.abcsys.devops.application.service;

import cn.abcsys.devops.application.model.ParamsApplication;
import cn.abcsys.devops.commons.utils.*;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.springframework.stereotype.Service;

/**
 * @author xuyuanjia2017@otcaix.iscsa.ac.cn
 * @date 2017/7/11 0011
 * say sth.
 */
@Service("checkService")
public class CheckService {

    public boolean checkCreateAppFromImage(ParamsApplication paramsApplication) {
        try{

            Boolean b1 = StringUtils.checkString(paramsApplication.applicationCore.getApplicationCoreName(),CheckPolicy.STRING_STRICT);
            System.out.println("getApplicationCoreName:" + b1);

            Boolean b2 = StringUtils.checkString(paramsApplication.applicationCore.getApplicationCoreUsername(),CheckPolicy.STRING_STRICT);
            System.out.println("getApplicationCoreUsername:"+b2);

            Boolean b3 = StringUtils.checkString(paramsApplication.applicationCore.getApplicationCoreType(),CheckPolicy.STRING_STRICT);
            System.out.println("getApplicationCoreType:"+b3);

            Boolean b4 = StringUtils.checkImageTag(paramsApplication.instanceImage.getConfigImageName(),paramsApplication.instanceImage.getConfigImageTag());
            System.out.println("getConfigImageName:"+b4);

            Boolean b5 = StringUtils.checkCpu(paramsApplication.configBasic.getConfigBasicCpu());
            System.out.println("getConfigBasicCpu:"+b5);

            Boolean b6 = StringUtils.checkMemory(paramsApplication.configBasic.getConfigBasicMemory());
            System.out.println("getConfigBasicMemory:"+b6);

            Boolean b7 = StringUtils.checkString(paramsApplication.configBasic.getConfigBasicNetwork(),CheckPolicy.STRING_ORIDNARY);
            System.out.println("getConfigBasicNetwork:"+b7);

            Boolean b8 = IPUtils.invalid(paramsApplication.ip);
            System.out.println("ip:"+b8);

            Boolean b9 = StringUtils.checkPorts(paramsApplication.configPortsListString);
            System.out.println("configPortsListString:"+b9);

            Boolean b10 = StringUtils.checkEnvList(paramsApplication.configEnvsListString);
            System.out.println("checkEnvList:"+b10);

            Boolean b11 = StringUtils.checkVolumesList(paramsApplication.configVolumesListString);
            System.out.println("configVolumesListString:"+b11);

            Boolean b12 = NumberUtils.isValid(paramsApplication.instanceReplica);
            System.out.println("instanceReplica:"+b12);

            Boolean b13 = NumberUtils.resetEnvId(paramsApplication);
            System.out.println("envId:"+b13);
            return  b1&& b2&& b3 && b4 && b5&& b6&& b7&& b8 && b9 && b10 && b11&& b12&&b13;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    public Boolean checkCreateRawApplication(ParamsApplication paramsApplication){
        try{
            Boolean b1 = StringUtils.checkString(paramsApplication.applicationCore.getApplicationCoreName(),CheckPolicy.STRING_STRICT);
            System.out.println("getApplicationCoreName:" + b1);

            Boolean b2 = StringUtils.checkString(paramsApplication.applicationCore.getApplicationCoreUsername(),CheckPolicy.STRING_STRICT);
            System.out.println("getApplicationCoreUsername:"+b2);

            Boolean b3 = StringUtils.checkString(paramsApplication.applicationCore.getApplicationCoreType(),CheckPolicy.STRING_STRICT);
            System.out.println("getApplicationCoreType:"+b3);

            Boolean b13 = NumberUtils.resetEnvId(paramsApplication);
            System.out.println("envId:"+b13);

            return b1&& b2&& b3 &&b13;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    public Boolean checkAddConfigVersion(ParamsApplication paramsApplication){
        try{
            Boolean b5 = StringUtils.checkCpu(paramsApplication.configBasic.getConfigBasicCpu());
            System.out.println("getConfigBasicCpu:"+b5);

            Boolean b6 = StringUtils.checkMemory(paramsApplication.configBasic.getConfigBasicMemory());
            System.out.println("getConfigBasicMemory:"+b6);

            Boolean b7 = StringUtils.checkString(paramsApplication.configBasic.getConfigBasicNetwork(),CheckPolicy.STRING_ORIDNARY);
            System.out.println("getConfigBasicNetwork:"+b7);

            Boolean b9 = StringUtils.checkPorts(paramsApplication.configPortsListString);
            System.out.println("configPortsListString:"+b9);

            Boolean b10 = StringUtils.checkEnvList(paramsApplication.configEnvsListString);
            System.out.println("checkEnvList:"+b10);

            Boolean b11 = StringUtils.checkVolumesList(paramsApplication.configVolumesListString);
            System.out.println("configVolumesListString:"+b11);

            Boolean b14 = NumberUtils.isValid(paramsApplication.applicationCore.getApplicationCoreId());
            System.out.println("getApplicationCoreId:"+b14);

//            Boolean b15 = NumberUtils.isValid(paramsApplication.cc.getConfigCoreId());
//            System.out.println("getApplicationCoreId:"+b15);

            return b5&& b6&& b7 && b9 && b10 && b11&& b14 ;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    public Boolean checkConstructForRawApplication(ParamsApplication paramsApplication){
        try{
            Boolean b4 = StringUtils.checkImageTag(paramsApplication.instanceImage.getConfigImageName(),paramsApplication.instanceImage.getConfigImageTag());
            System.out.println("getConfigImageName:"+b4);

            Boolean b5 = StringUtils.checkCpu(paramsApplication.configBasic.getConfigBasicCpu());
            System.out.println("getConfigBasicCpu:"+b5);

            Boolean b6 = StringUtils.checkMemory(paramsApplication.configBasic.getConfigBasicMemory());
            System.out.println("getConfigBasicMemory:"+b6);

            Boolean b7 = StringUtils.checkString(paramsApplication.configBasic.getConfigBasicNetwork(),CheckPolicy.STRING_ORIDNARY);
            System.out.println("getConfigBasicNetwork:"+b7);

            Boolean b8 = IPUtils.invalid(paramsApplication.ip);
            System.out.println("ip:"+b8);

            Boolean b9 = StringUtils.checkPorts(paramsApplication.configPortsListString);
            System.out.println("configPortsListString:"+b9);

            Boolean b10 = StringUtils.checkEnvList(paramsApplication.configEnvsListString);
            System.out.println("checkEnvList:"+b10);

            Boolean b11 = StringUtils.checkVolumesList(paramsApplication.configVolumesListString);
            System.out.println("configVolumesListString:"+b11);

            Boolean b12 = NumberUtils.isValid(paramsApplication.instanceReplica);
            System.out.println("instanceReplica:"+b12);

            Boolean b14 = NumberUtils.isValid(paramsApplication.applicationCore.getApplicationCoreId());
            System.out.println("getApplicationCoreId:"+b14);

//            Boolean b15 = NumberUtils.isValid(paramsApplication.cc.getConfigCoreId());
//            System.out.println("getApplicationCoreId:"+b15);

            return b4 && b5&& b6&& b8 &&b7 && b9 && b10 && b11&& b12 && b14;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }
}
